#include <cmath>
#include <iostream>

float calculate_angle_difference(float angle1, float angle2)
{
    angle1 = fmod(angle1, 360.0);
    if (angle1 < 0)
        angle1 += 360.0;

    angle2 = fmod(angle2, 360.0);
    if (angle2 < 0)
        angle2 += 360.0;

    float diff = abs(angle1 - angle2);

    // 确保是最小环形差值
    if (diff > 180.0) {
        diff = 360.0 - diff;
    }

    return diff;
}

int main()
{
     std::cout << "-180 2 --> " << calculate_angle_difference(-180.0, 2.0) << std::endl;
     std::cout << "2 -180 --> " << calculate_angle_difference(2, -180) << std::endl;
     std::cout << "-160 -180 --> " << calculate_angle_difference(-160, -180) << std::endl;
     std::cout << "-180 -160 --> " << calculate_angle_difference(-180, -160) << std::endl;
}
